package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.service.ISysDictDataService;
import com.huike.clues.service.ITbAssignRecordService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Resource
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Resource
    private TbAssignRecordMapper assignRecordMapper;

    @Resource
    private TbBusinessMapper tbBusinessMapper;

    @Override
    public AjaxResult add(BusinessTrackVo businessTrackVo) {
        if (StringUtils.isEmpty(businessTrackVo.getName())) {
            return AjaxResult.error("请输入姓名");
        }

        if (StringUtils.isEmpty(businessTrackVo.getPhone())) {
            return AjaxResult.error("请输入手机号");
        }

        if (StringUtils.isEmpty(businessTrackVo.getSubject())) {
            return AjaxResult.error("请输入意向学科");
        }

        if (businessTrackVo.getCourseId() == null) {
            return AjaxResult.error("请输入意向课程");
        }

        if (StringUtils.isEmpty(businessTrackVo.getTrackStatus())) {
            return AjaxResult.error("请输入跟进状态");
        }


        if (businessTrackVo.getNextTime() == null) {
            return AjaxResult.error("请输入下次跟进时间");
        }

        if (StringUtils.isEmpty(businessTrackVo.getKeyItems())) {
            return AjaxResult.error("请输入沟通重点");
        }

        if (StringUtils.isEmpty(businessTrackVo.getOtherIntention())) {
            return AjaxResult.error("请输入其他意向");
        }

        String username = SecurityUtils.getUsername();

        businessTrackVo.setCreateBy(username);

        int add = tbBusinessTrackRecordMapper.add(businessTrackVo);
        if (add == 0) {
            return AjaxResult.error("添加失败");
        }
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult list(Long id) {
        List<TbBusinessTrackRecord> list = tbBusinessTrackRecordMapper.list(id);

        String dictType = "communication_point";
        for (TbBusinessTrackRecord tbBusinessTrackRecord : list) {
            String[] split = tbBusinessTrackRecord.getKeyItems().split(",");
            for (String s : split) {
                String label = sysDictDataService.selectDictLabel(dictType, s);
                tbBusinessTrackRecord.getKeys().add(label);
            }
        }

        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult back(Long id, String value) {
        BusinessTrackVo tbBusinessTrackRecord = new BusinessTrackVo();
        //添加操作人
        String username = SecurityUtils.getUsername();
        tbBusinessTrackRecord.setCreateBy(username);
        //添加操作商机id和关键字
        tbBusinessTrackRecord.setKeyItems(value);
        tbBusinessTrackRecord.setBusinessId(id);

        tbBusinessTrackRecordMapper.add(tbBusinessTrackRecord);
        //删除分配记录表中该id
        assignRecordMapper.deleteAssignRecordByAssignId(id);

        //修改商机状态
        TbBusiness tbBusiness = tbBusinessMapper.selectTbBusinessById(id);
        tbBusiness.setStatus("3");
        tbBusinessMapper.updateTbBusiness(tbBusiness);

        return AjaxResult.success("操作成功");
    }


}
